<template>
  <div class="home">
    <page-header class="box_fixed" :class="{'is_fixed' : isFixed}" :model="model"></page-header>
    <nav-bar :options="options"></nav-bar>
    <div class="fixed-openapp">
      <i class="iconfont icon-bilibili"></i>
      <span>打开App，看你感兴趣的视频</span>
    </div>
    <div class="copyright">
      <p>信息网络传播视听节目许可证：0910417</p>
      <p>网络文化经营许可证 沪网文【2019】3804-274号</p>
      <p>广播电视节目制作经营许可证：（沪）字第01248号</p>
      <p>增值电信业务经营许可证 沪B2-20100043</p>
    </div>
  </div>
</template>

<script>
import PageHeader from '../components/common/PageHeader'
import NavBar from '../components/common/NavBar'
export default {
  components: {
    PageHeader,
    NavBar
  },
  data () {
    return {
      model: {},
      options: [],
      isFixed: false,
      offsetTop: 0
    }
  },
  mounted () {
    window.addEventListener('scroll', this.initHeight)
    this.$nextTick(() => {
      this.offsetTop = document.querySelector('.box_fixed').offsetTop
    })
  },
  methods: {
    initHeight () {
      // 设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 (被卷曲的高度)
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      // 如果被卷曲的高度大于吸顶元素到顶端位置 的距离
      this.isFixed = scrollTop > this.offsetTop
    },
    async fetchUser () {
      const res = await this.$http.get('/user')
      this.model = res.data
    },
    async fetchCategories () {
      const { data } = await this.$http.get('/category')
      // const res = data.filter(el => el.hasOwnProperty('parent'))
      const res = data.filter(el => !Object.prototype.hasOwnProperty.call(el, 'parent'))
      res.unshift({ name: '首页' })
      this.options = res
    }
  },
  created () {
    this.fetchUser()
    this.fetchCategories()
  },
  destroyed () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="scss">
body,
html {
  background-color: #f4f4f4 !important;
}
.home {
  display: flex;
  height: 100vh;
  background-color: #fff;
  flex-direction: column;
  .fixed-openapp {
    position: fixed;
    bottom: 35px;
    left: 50%;
    transform: translateX(-50%);
    height: 35px;
    width: 337px;
    background-color: #fb7299;
    text-align: center;
    line-height: 35px;
    color: #fff;
    border-radius: 17.5px;
    font-size: 13.4px;
    z-index: 999;
    box-shadow: 0 2px 9px rgba(0, 0, 0, 0.2);
    .iconfont {
      padding-right: 10px;
    }
  }
  .is_fixed {
    position: fixed;
    top: 0;
    z-index: 999;
  }

  .copyright {
    margin: 15px 0;
    text-align: center;
    p {
      color: #999;
      font-size: 12px;
      line-height: 24px;
    }
  }
}
</style>
